package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallAd;
import com.cskaoyan.bean.CskaoyanmallAdExample;
import com.cskaoyan.bean.bo.ExtensionAdBo;
import com.cskaoyan.bean.vo.AdVo;
import com.cskaoyan.mapper.CskaoyanmallAdMapper;
import com.cskaoyan.service.ExtensionAdvertisementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import sun.font.ScriptRun;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 推广管理
 *      广告管理
 *          by 桂圆
 */
@Service
public class ExtensionAdvertisementServiceImpl implements ExtensionAdvertisementService {

    @Autowired
    CskaoyanmallAdMapper cskaoyanmallAdMapper;

    /**
     * 查询所有的广告
     */
//    @Override
//    public List<CskaoyanmallAd> queryAdList(Integer page, Integer limit, String sort, String order) {
//        PageHelper.startPage(page, limit);
//
//        CskaoyanmallAdExample cskaoyanmallAdExample = new CskaoyanmallAdExample();
//        cskaoyanmallAdExample.setOrderByClause(sort + " " + order);
//
//        List<CskaoyanmallAd> cskaoyanmallAds = cskaoyanmallAdMapper.selectByExample(cskaoyanmallAdExample);
//
//        return cskaoyanmallAds;
//    }

    /**
     * 模糊查询广告标题和广告内容
     */
    @Override
    public AdVo vagueQueryAds(Integer page, Integer limit, String sort, String order, String name, String content) {

        // TODO:
        PageHelper.startPage(page, limit);

        CskaoyanmallAdExample adExample = new CskaoyanmallAdExample();
        adExample.setOrderByClause(sort + " " + order);

        CskaoyanmallAdExample.Criteria criteria = adExample.createCriteria();


        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (content != null && !"".equals(content)) {
            criteria.andContentLike("%" + content + "%");
        }

        List<CskaoyanmallAd> Ads = cskaoyanmallAdMapper.selectByExample(adExample);

        Iterator<CskaoyanmallAd> iterator = Ads.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().getDeleted()) {
                iterator.remove();
            }
        }

        PageInfo<CskaoyanmallAd> adPageInfo = new PageInfo<>(Ads);
        long total = adPageInfo.getTotal();

        AdVo adVo = new AdVo();
        adVo.setTotal(total);
        adVo.setItems(Ads);

        return adVo;
    }

    /**
     * 删除广告--逻辑删除
     */
    @Override
    public Integer deleteAdvertisement(CskaoyanmallAd adBo) {

        adBo.setDeleted(true);
        Integer i = cskaoyanmallAdMapper.updateByPrimaryKey(adBo);

        return i;
    }

    /**
     * 添加广告
     */
    @Override
    public CskaoyanmallAd addAdvertisement(ExtensionAdBo adBo) {

        CskaoyanmallAd cskaoyanmallAd = new CskaoyanmallAd();
        cskaoyanmallAd.setName(adBo.getName());
        cskaoyanmallAd.setLink(adBo.getLink());
        cskaoyanmallAd.setUrl(adBo.getUrl());
        cskaoyanmallAd.setPosition(new Byte(String.valueOf(adBo.getPosition())));
        cskaoyanmallAd.setContent(adBo.getContent());
        cskaoyanmallAd.setEnabled(adBo.getEnabled());
        cskaoyanmallAd.setAddTime(new Date());
        cskaoyanmallAd.setUpdateTime(new Date());

        cskaoyanmallAdMapper.insertSelective(cskaoyanmallAd);

        CskaoyanmallAdExample adExample = new CskaoyanmallAdExample();
        CskaoyanmallAdExample.Criteria criteria = adExample.createCriteria();
        criteria.andUrlEqualTo(adBo.getUrl());
        List<CskaoyanmallAd> cskaoyanmallAds = cskaoyanmallAdMapper.selectByExample(adExample);
        Integer id = cskaoyanmallAds.get(0).getId();

        cskaoyanmallAd.setId(id);

        return cskaoyanmallAd;
    }

    @Override
    public void updateAd(ExtensionAdBo adBo) {

        String addTime = adBo.getAddTime().replaceAll("T", " ").replaceAll("\\..*", "");
        String updateTime = adBo.getUpdateTime().replaceAll("T", " ").replaceAll("\\..*", "");
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date addTimeNew = null;
        Date updateTimeNew = null;
        try {
            addTimeNew = sf.parse(addTime);
            updateTimeNew = sf.parse(updateTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

        CskaoyanmallAd cskaoyanmallAd = new CskaoyanmallAd(adBo.getId(), adBo.getName(), adBo.getLink(), adBo.getUrl(),
                (byte) adBo.getPosition().intValue(), adBo.getContent(), null, null, adBo.getEnabled(),
                addTimeNew, updateTimeNew, adBo.getDeleted());

        cskaoyanmallAdMapper.updateByPrimaryKey(cskaoyanmallAd);
    }
}
